package com.yc.storage.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.yc.api.CommonResult;
import com.yc.service.IRedisService;
import com.yc.storage.dto.FileStorageDto;
import com.yc.storage.service.IQiniuService;
import com.yc.util.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1")
public class UploadController {

    @Value("${file.path}")
    private String path;
    @Value("${file.domain}")
    private String domain;
    @Value("${qiniu.domain}")
    private String qiniuDomain;
    private final IRedisService redisService;
    private final IQiniuService qiniuService;

    @PostMapping("/upload")
    public CommonResult<?> upload(@RequestParam("file") MultipartFile file) {
        String realName = file.getOriginalFilename();
        String extension = FileUtil.getExtensionName(realName);
        String fileType = FileUtil.getFileType(extension);
        String filePath = path + fileType;
        File tFile = FileUtil.upload(file, filePath);
        if (ObjectUtil.isNotNull(tFile)) {
            assert tFile != null;
            String localPath = fileType + File.separator + tFile.getName();
            FileStorageDto storageDto = FileStorageDto.builder()
                    .path(localPath)
                    .realName(realName)
                    .name(tFile.getName())
                    .md5(FileUtil.getFileNameNoEx(tFile.getName()))
                    .size(tFile.length())
                    .suffix(extension)
                    .build();
            String url = domain + localPath;
            redisService.set(url, storageDto, 1L, TimeUnit.DAYS);
            return CommonResult.success(url);
        }
        return CommonResult.failed();
    }

    @PostMapping("/upload-qiniu")
    public CommonResult<?> uploadQiniu(@RequestParam("file") MultipartFile file) throws IOException {
        String realName = file.getOriginalFilename();
        String extension = FileUtil.getExtensionName(realName);
        String fileType = FileUtil.getFileType(extension);

        String md5Str = SecureUtil.md5(file.getInputStream());
        String fileName = md5Str + "." + extension;
        String path = fileType + File.separator + fileName;

        boolean isOk = qiniuService.uploadFile(file.getInputStream(), path);
        if (isOk) {
            FileStorageDto storageDto = FileStorageDto.builder()
                    .path(path)
                    .realName(realName)
                    .name(fileName)
                    .md5(md5Str)
                    .size(file.getSize())
                    .suffix(extension)
                    .build();
            String url = qiniuDomain + path;
            redisService.set(url, storageDto, 1L, TimeUnit.DAYS);
            return CommonResult.success(url);
        }
        return CommonResult.failed();
    }
}
